<?php
/**
 * Created by JetBrains PhpStorm.
 * User: Administrator
 * Date: 14-5-8
 * Time: 下午4:26
 * To change this template use File | Settings | File Templates.
 */
include_once dirname(__FILE__) . '/../constant/constant.php';
include_once dirname(__FILE__) . '/../../lib/Log4PHP/Log4PHP.php';
include_once dirname(__FILE__) . '/../common/dbConn.php';

global $msgArray;
global $log;
;

$log = Log4PHP::getLogger("UserRegisterLog");
$msgArray = new MsgArray();

function UserRegister_Route($arg_body){
    try{
        global $log;
        global $msgArray;
        $log->info("===================UserRegister_Route begin=====================");
        $log->info($arg_body);
        $rsl = $msgArray->getMsgArray(1,"操作失败!",[],"Warn");
        if(!array_key_exists("route",$arg_body)){
            return $msgArray->getMsgArray(404,"传入路由为空",[],"Warn");
        }
        switch ($arg_body["route"]){
            case "UserRegister/register":$rsl=register_func($arg_body);break;

            default;break;
        }
        return $rsl;
    }catch (Exception $e){
        $log->error("抛出异常:".$e->getMessage());
        return $msgArray->getMsgArray(100001,"系统异常",[],"Error");
    }
}
function register_func($arg_body){
    try{
        global $log;
        global $msgArray;
        $log->info("===================register_func begin=====================");
        $log->info("传入参数：");
        $log->info($arg_body);
        $rsl = $msgArray->getMsgArray(1,"操作失败!",[],"Warn");
        if(!array_key_exists("userPhone",$arg_body)){
            return $msgArray->getMsgArray(201,"传入电话参数不存在",[],"Warn");
        }
        if(!array_key_exists("userSchool",$arg_body)){
            return $msgArray->getMsgArray(202,"传入学校参数不存在",[],"Warn");
        }
        if(!array_key_exists("userNickName",$arg_body)){
            return $msgArray->getMsgArray(203,"传入昵称参数不存在",[],"Warn");
        }
        if(!array_key_exists("identity",$arg_body)){
            return $msgArray->getMsgArray(204,"传入身份标识参数不存在",[],"Warn");
        }
        if($arg_body["userPhone"]==""||$arg_body["userSchool"]==""||$arg_body["userNickName"]==""||$arg_body["identity"]==""){
            return $msgArray->getMsgArray(205,"传入参数为空！","","Error");
        }
        $q_pdo=connectDatabase();
        if(null==$q_pdo){
            return $msgArray->getMsgArray(1000,"数据库连接异常",[],"Warn");
        }
        $q_pdo->beginTransaction();
        $checkUserIsExistSql="select id from t_user where idenity='".$arg_body["identity"]."'";
        $insertIntoUserSql="INSERT INTO  t_user (user_phone,user_school,user_nickname,idenity)values (?,?,?,?)";

        $stmt= $q_pdo->query($checkUserIsExistSql);
        $stmt->setFetchMode(PDO::FETCH_ASSOC);
        $rsl=$stmt->fetchAll();
        if(count($rsl)>0){
            return $msgArray->getMsgArray(1002,"用户已注册",[],"Warn");
        }



        $log->info($insertIntoUserSql.",".$arg_body["userPhone"].",".$arg_body["userSchool"].",".$arg_body["userNickName"].",".$arg_body["identity"]);
        $stmt = $q_pdo->prepare($insertIntoUserSql);
        $num=$stmt->execute(array($arg_body["userPhone"],$arg_body["userSchool"],$arg_body["userNickName"],$arg_body["identity"]));
        if($num<=0){
            return $msgArray->getMsgArray(1001,"注册失败！","","Error");
        }
        $q_pdo->commit();
        return $msgArray->getMsgArray(0,"注册成功",[],"Success");

    }catch (Exception $e){
        $log->error("抛出异常:".$e->getMessage());
        return $msgArray->getMsgArray(100001,"系统异常",[],"Error");
    }
}
